#pragma once

#include "Singleton.h"

#include "const.h"

#include <functional>
#include <memory>
#include <thread>
#include <string>
#include <queue>
#include <condition_variable>
#include <map>
#include <unordered_map>

class CSession;
class LogicNode;
// class UserInfo;

using FunCallback = std::function<void(std::shared_ptr<CSession>, const short& msg_id, const std::string& msg_data)>;

class LogicSystem : public Singleton<LogicSystem>
{
	friend class Singleton<LogicSystem>;
public:
	~LogicSystem();

	void postMsgToQue(std::shared_ptr<LogicNode> msg);

private:
	LogicSystem();


	void dealMsg();
	void processMessage(std::shared_ptr<LogicNode> msg_node);
	// 注册对应消息id的回调函数
	void registerCallbacks();
	// 进行登录消息的回调
	void loginHandler(std::shared_ptr<CSession> session, const short& msg_id, const std::string& msg_data);

private:
	std::queue<std::shared_ptr<LogicNode>> m_msg_que;
	std::mutex m_mtex;
	std::condition_variable m_cond;
	bool m_b_stop;
	std::map<unsigned short, FunCallback> m_fun_callbacks_map; // id与其回调函数的映射
	std::unordered_map<int, std::shared_ptr<UserInfo>> m_users;
	std::thread m_worker_thread;
};

